package org.example.springbook.service;

import org.example.springbook.enums.BookStatusEnums;
import org.example.springbook.mapper.BookMapper;
import org.example.springbook.model.BookInfo;
import org.example.springbook.model.PageRequest;
import org.example.springbook.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;
    public Integer insertBook(BookInfo bookInfo){
        return bookMapper.insertBook(bookInfo);
    }
    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
       //这里进行封装数据
        Integer count=bookMapper.getCount();
        List<BookInfo> bookInfos = bookMapper.queryListBypage(pageRequest);
        for (BookInfo bookInfo : bookInfos) {
            if(bookInfo.getStatus()==0){
                //这里如果对这个描述进行写死的话，我们在其他地方也进行引用的话，容易忘记修改引用的地方
                bookInfo.setStatusCN(BookStatusEnums.getEnumsByCode(bookInfo.getStatus()).getDesc());
            }
        }
        return new PageResult<BookInfo>(count,bookInfos);
    }
    public BookInfo queryBookById(Integer id) {
        //直接进行查询，控制层会进行参数检验
        return bookMapper.queryBookById(id);
    }

    public Integer updateBook(BookInfo bookInfo) {
        //服务层和控制层返回的结果不同
        //服务层更接近数据，返回的一般是有几行数据收到影响
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
